package com.jeeplus.modules.guide.task;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.guide.entity.MobileUserYY;
import com.jeeplus.modules.guide.service.*;
import com.jeeplus.modules.guide.utils.*;
import com.jeeplus.modules.sys.entity.SysConfig;
import com.jeeplus.modules.sys.service.SysConfigService;
import com.jeeplus.modules.sys.service.UserService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import com.jeeplus.common.utils.CacheUtils;
import okhttp3.*;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/*
异步/公共发送请求类
*/
@Component
public class Send {
    @Autowired
    private MobileSilentService mobileSilentService;
    @Autowired
    private MobileBusinessService mobilebusinessService;
    @Autowired
    private  MobileLoginService mobileLoginService;
    @Autowired
    private MobileQueueService mobileQueueService;
    @Autowired
    private MobilePublicSecurityService mobilePublicSecurityService;
    @Autowired
    private  WeixinUtil weixinUtil;
    @Autowired
    private  RongYiBanService rongYiBanService;
    @Autowired
    private  SWService swService;
    @Autowired
    private  TextMessageWarnService textMessageWarnService;
    @Autowired
    private  MobileIndexService mobileIndexService;
    @Autowired
    private UserService userService;
    private static final Logger logger = LoggerFactory.getLogger(Send.class);
    private static String SMS_ID_START =  "3";//取号排队短信发送 短信模板id
    private static String SMS_ID_END =  "4";//加号短信发送 短信模板id
    private static String SMS_ID_SEND =  "2";//排队信息短信发送 短信模板id
    private final String CS_OPENID = "oyyT9syc9Fybl6DkExdLAJlonLDE";
    private static String name =  "zwfwc";//短信用户名
    private static String pwd =  "ul@2022";//短信密码
    //private static String name =  "xzwzx";//短信用户名
    //private static String pwd =  "zwzx#2021";//短信密码
    private static String results = "【成都高新区】";
    //排号语音接口
    @Value("${voice}")
    private String voice;
    //用户扫描排号机二维码跳转url
    @Value("${toUrl}")
    private String toUrl;
    //公安KEY
    @Value("${gongAnKey}")
    private String gongAnKey;
    //手机评价跳转url
    @Value("${PJURL}")
    private String PJURL;
    //蓉易办-排队叫号-区县上传取号数据
    @Value("${GetNumber}")
    private String GetNumber="http://10.31.30.68:5858/gateway/api/1/ryb/pdjh/qxscqhsj";
    //蓉易办-排队叫号-区县取号办理信息上传
    @Value("${HandleInfoUp}")
    private String HandleInfoUp="http://10.31.30.68:5858/gateway/api/1/ryb/pdjh/qxqhblxxsc";
    //高新行政区划代码
    private String AreaCode="510109";
    //蓉易办对接Appkey
    @Value("${AppKey}")
    private String AppKey;
    //短信url
    @Value("${emsUrl}")
    private String emsUrl;
    //手机评价详情跳转url
    @Value("${PJURLXQ}")
    private String PJURLXQ;
    //差评推送openid
    @Value("${cpOpenid}")
    private String cpOpenid;
    /**
     *
     * 异步调用群众微信基础信息记录
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    public void getWXInfo() {
        //验证token有效性
        String url = WeiXinConfig.weixin_user_info.replace("ACCESS_TOKEN", CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString()).replace("OPENID", CS_OPENID);
        String result = "";
        try{
            result = HttpRequestUtil.sendPost(url, null);
            if (result == null || result.contains("errcode")) {
                weixinUtil.getAccessToken();
            }
        }catch (Exception e){
            logger.error("微信token失效！");
        }
    }


    public void findUserSMSPJ(){
        logger.error("-------开始推送微信评价通知------");
        List<HashMap<String,String>> list = mobilebusinessService.findUserSMSPJ();
        this.getWXInfo();
        for(HashMap<String,String> m : list){
            if(null != m.get("openid")){
                logger.error(m.get("id")+"已推送微信评价");
                //if(mobileQueueService.findCountCounter(m.get("id")) <= 0)
                this.toUserPJ(m.get("openid"),m.get("number"),m.get("typeName"),m.get("id"));
                mobilebusinessService.updateUserSMSPJ(m.get("id"));
            }
        }
    }
    /**
     *
     * 推送当前微信用户排队数据
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendWXInfo(String openid,String number,Map<String, Object> m,String tell,String areaId) {
        if(null != m){
            Map<String,Object> map = new HashMap();
            map.put("template_id", WeiXinConfig.successQueueId);// 模板消息id
            if(number.indexOf("(") >= 0){
                number = number.substring(0,number.indexOf("("));
            }
            //isSend = 0 已发送
            map.put("url",toUrl+"?isSend="+WeiXinConfig.isSend+"&areaId="+areaId+"&number="+number+"&tell="+tell);
            // 封装data
            Map<String, Map> dataMap = new HashMap<String, Map>();
            Map<String, String> firstMap = new HashMap<String, String>();
            firstMap.put("value", "尊敬的群众您好，排队信息如下（点击可查看当前时时排队情况）"); // 内容value
            firstMap.put("color", "#173177"); // 字体颜色
            // 封装remark 备注
            Map<String, String> remark = new HashMap<String, String>();
            remark.put("value", "目前大厅已启用无声叫号，请关注大厅叫号信息。以免过号，谢谢合作!");
            remark.put("color", "#888888");
            // 封装keyword1 办理业务
            Map<String, Object> keyword1Map = new HashMap<String, Object>();
            keyword1Map.put("color", "#888888");
            // 封装keyword2 排队时间
            Map<String, Object> keyword2Map = new HashMap<String, Object>();
            keyword2Map.put("color", "#888888");
            // 封装keyword3 排队号码
            Map<String, Object> keyword3Map = new HashMap<String, Object>();
            keyword3Map.put("color", "#888888");
            // 封装keyword4 等待人数
            Map<String, Object> keyword4Map = new HashMap<String, Object>();
            keyword4Map.put("color", "#888888");
            // 封装keyword5 办理地点
            Map<String, Object> keyword5Map = new HashMap<String, Object>();
            keyword5Map.put("color", "#888888");

            map.put("touser", openid);// 您要发送给某个用户的openid
            keyword1Map.put("value", m.get("typeName"));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            keyword2Map.put("value", sdf.format(new Date()));
            Map <String, String> numberValue = mobileLoginService.findUserByOpendId(openid);
            if(null != numberValue){
                keyword3Map.put("value", number+"（"+getTM(numberValue.get("name"),numberValue.get("idcard"))+"）");
            }else{
                keyword3Map.put("value", number);
            }
            keyword4Map.put("value", m.get("num"));
            keyword5Map.put("value", m.get("areaName"));
            dataMap.put("first", firstMap);
            dataMap.put("keyword1", keyword1Map);
            dataMap.put("keyword2", keyword2Map);
            dataMap.put("keyword3", keyword3Map);
            dataMap.put("keyword4", keyword4Map);
            dataMap.put("keyword5", keyword5Map);
            dataMap.put("remark", remark);
            map.put("data", dataMap);
            String result = HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage + CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());
            //调用记录
            userService.intoUserHistory(openid,"线上取号微信排队信息推送","返回结果:"+result,number,m.get("areaName").toString(),"无");
        }
    }

    /**
     *
     * 推送当前微信用户排队数据
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendAppointmentInfo(String openid,String number,String typeName,String num, String areaName,String winNo,String doTime) {
            Map<String,Object> map = new HashMap();
            map.put("template_id", WeiXinConfig.successQueueId);// 模板消息id
            // 封装data
            Map<String, Map> dataMap = new HashMap<String, Map>();
            Map<String, String> firstMap = new HashMap<String, String>();
            firstMap.put("value", "尊敬的群众您好，您已成功预约取号"); // 内容value
            firstMap.put("color", "#173177"); // 字体颜色
            // 封装remark 备注
            Map<String, String> remark = new HashMap<String, String>();
            remark.put("value", "请的办理窗口号为["+winNo+"]。请随时关注叫号信息。");
            remark.put("color", "#888888");
            // 封装keyword1 办理业务
            Map<String, Object> keyword1Map = new HashMap<String, Object>();
            keyword1Map.put("color", "#888888");
            // 封装keyword2 排队时间
            Map<String, Object> keyword2Map = new HashMap<String, Object>();
            keyword2Map.put("color", "#888888");
            // 封装keyword3 排队号码
            Map<String, Object> keyword3Map = new HashMap<String, Object>();
            keyword3Map.put("color", "#888888");
            // 封装keyword4 等待人数
            Map<String, Object> keyword4Map = new HashMap<String, Object>();
            keyword4Map.put("color", "#888888");
            // 封装keyword5 办理地点
            Map<String, Object> keyword5Map = new HashMap<String, Object>();
            keyword5Map.put("color", "#888888");
            map.put("touser", openid);// 您要发送给某个用户的openid
            keyword1Map.put("value", typeName);
            keyword2Map.put("value", doTime);
            keyword3Map.put("value", number);
            keyword4Map.put("value", num);
            keyword5Map.put("value", areaName);
            dataMap.put("first", firstMap);
            dataMap.put("keyword1", keyword1Map);
            dataMap.put("keyword2", keyword2Map);
            dataMap.put("keyword3", keyword3Map);
            dataMap.put("keyword4", keyword4Map);
            dataMap.put("keyword5", keyword5Map);
            dataMap.put("remark", remark);
            map.put("data", dataMap);
            HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());
    }

    /**
     *
     * 推送当前微信用户预约数据
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendAppointmentValue(String idcard,String typeName,String areaName,Date startTime) {
        Map<String,Object> map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("YYY-MM-dd HH:mm:ss");//格式化时间显示
        map.put("template_id", WeiXinConfig.successId);// 模板消息id
        // 封装data
        Map<String, Map> dataMap = new HashMap<String, Map>();
        Map<String, String> firstMap = new HashMap<String, String>();
        firstMap.put("value", "尊敬的群众您好，您已预约成功"); // 内容value
        firstMap.put("color", "#173177"); // 字体颜色
        // 封装remark 备注
        Map<String, String> remark = new HashMap<String, String>();
        remark.put("value", "请在高新服务[高馨办]的个人中心查看预约详情！");
        remark.put("color", "#888888");
        // 封装keyword1 办理地点
        Map<String, Object> keyword1Map = new HashMap<String, Object>();
        keyword1Map.put("color", "#888888");
        // 封装keyword2 办理业务排队时间
        Map<String, Object> keyword2Map = new HashMap<String, Object>();
        keyword2Map.put("color", "#888888");
        // 封装keyword3 排队时间
        Map<String, Object> keyword3Map = new HashMap<String, Object>();
        keyword3Map.put("color", "#888888");
        map.put("touser", mobileLoginService.getOpenid(idcard));// 您要发送给某个用户的openid
        keyword1Map.put("value", areaName);
        typeName = typeName.replace("(预约)","");
        keyword2Map.put("value", typeName);
        keyword3Map.put("value", sdf.format(startTime));
        dataMap.put("first", firstMap);
        dataMap.put("keyword1", keyword1Map);
        dataMap.put("keyword2", keyword2Map);
        dataMap.put("keyword3", keyword3Map);
        dataMap.put("remark", remark);
        map.put("data", dataMap);
        HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());
    }

    /**
     *
     * 推送当前微信用户预约数据
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendAppointmentValueXJH(String idcard,String typeName,String areaName) {
        Map<String,Object> map = new HashMap();
        map.put("template_id", WeiXinConfig.successId);// 模板消息id
        // 封装data
        Map<String, Map> dataMap = new HashMap<String, Map>();
        Map<String, String> firstMap = new HashMap<String, String>();
        firstMap.put("value", "尊敬的群众您好，您已预约成功"); // 内容value
        firstMap.put("color", "#173177"); // 字体颜色
        // 封装remark 备注
        Map<String, String> remark = new HashMap<String, String>();
        remark.put("value", "请在肖+政务的[个人服务]查看预约详情或进行二维码取号！");
        remark.put("color", "#888888");
        // 封装keyword1 办理地点
        Map<String, Object> keyword1Map = new HashMap<String, Object>();
        keyword1Map.put("color", "#888888");
        // 封装keyword2 办理业务排队时间
        Map<String, Object> keyword2Map = new HashMap<String, Object>();
        keyword2Map.put("color", "#888888");
        // 封装keyword3 排队时间
        Map<String, Object> keyword3Map = new HashMap<String, Object>();
        keyword3Map.put("color", "#888888");
        map.put("touser", mobileLoginService.getOpenid(idcard));// 您要发送给某个用户的openid
        keyword1Map.put("value", areaName);
        typeName = typeName.replace("(预约)","");
        keyword2Map.put("value", typeName);
        keyword3Map.put("value", DateUtils.getDateTime());
        dataMap.put("first", firstMap);
        dataMap.put("keyword1", keyword1Map);
        dataMap.put("keyword2", keyword2Map);
        dataMap.put("keyword3", keyword3Map);
        dataMap.put("remark", remark);
        map.put("data", dataMap);
        HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());
    }


    /**
     *
     * 通过openid,number,winNo推送叫号数据 微信推送
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendWX(Map<String,Object> maps,String winNo,String number) {
        Map<String,Object> map = new HashMap();
        map.put("template_id", WeiXinConfig.queueId);// 模板消息id
        // 封装data
        Map<String, Map> dataMap = new HashMap<String, Map>();
        Map<String, String> firstMap = new HashMap<String, String>();
        firstMap.put("value", "您好，您的排队号已呼叫。"); // 内容value
        firstMap.put("color", "#173177"); // 字体颜色
        // 封装remark 备注
        Map<String, String> remark = new HashMap<String, String>();
        remark.put("value", "请尽快前往窗口"+winNo+"办理业务，\n" + "以免过号，谢谢合作!");
        remark.put("color", "#888888");
        // 封装keyword1 排队号码
        Map<String, Object> keyword1Map = new HashMap<String, Object>();
        keyword1Map.put("color", "#888888");
        // 封装keyword2 业务名称
        Map<String, Object> keyword2Map = new HashMap<String, Object>();
        keyword2Map.put("color", "#888888");
        // 封装keyword3 办理窗口
        Map<String, Object> keyword3Map = new HashMap<String, Object>();
        keyword3Map.put("color", "#888888");
        // 封装keyword4 呼叫时间
        Map<String, Object> keyword4Map = new HashMap<String, Object>();
        keyword4Map.put("color", "#888888");
        // 封装keyword5 取号时间
        Map<String, Object> keyword5Map = new HashMap<String, Object>();
        keyword5Map.put("color", "#888888");
        //判断是否关注 和获取用户openid
        if(null != maps.get("openid")){
            if(null == maps.get("typeName"))maps.put("typeName","暂无数据");
            if(null == maps.get("getNumberTime"))maps.put("getNumberTime","暂无数据");
            map.put("touser", maps.get("openid"));// 您要发送给某个用户的openid
            keyword1Map.put("value", number);
            keyword2Map.put("value", maps.get("typeName"));
            keyword3Map.put("value", winNo);
            keyword4Map.put("value", DateUtils.getDateTime());
            keyword5Map.put("value", maps.get("getNumberTime").toString());
            dataMap.put("first", firstMap);
            dataMap.put("keyword1", keyword1Map);
            dataMap.put("keyword2", keyword2Map);
            dataMap.put("keyword3", keyword3Map);
            dataMap.put("keyword4", keyword4Map);
            dataMap.put("keyword5", keyword5Map);
            dataMap.put("remark", remark);
            map.put("data", dataMap);
            String result = HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());
            //调用记录
            userService.intoUserHistory(maps.get("openid").toString(),"线下叫号微信排队信息推送","返回结果:"+result,number,winNo,"无");
        }
    }

    /**
     *
     * 通过areaId,number异步推送取号排队提醒 微信推送
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendLineWX(Map<String,Object> m) {
        Map<String,Object> map = new HashMap();
        // 封装data
        Map<String, Map> dataMap = new HashMap<String, Map>();
        Map<String, String> firstMap = new HashMap<String, String>();
        firstMap.put("value", "您好，即将叫到您的号码。"); // 内容value
        firstMap.put("color", "#173177"); // 字体颜色
        // 封装keyword1 业务类型
        Map<String, Object> keyword1Map = new HashMap<String, Object>();
        keyword1Map.put("color", "#888888");
        // 封装keyword2 排队号码
        Map<String, Object> keyword2Map = new HashMap<String, Object>();
        keyword2Map.put("color", "#888888");
        // 封装keyword3 前面人数
        Map<String, Object> keyword3Map = new HashMap<String, Object>();
        keyword3Map.put("color", "#888888");
        // 封装remark 备注
        Map<String, String> remark = new HashMap<String, String>();
        keyword2Map.put("value", "目前大厅已启用无声叫号，请及时到办理大厅等待以免过号，谢谢合作!");
        keyword2Map.put("color", "#888888");
        if(null != m.get("openid")){
            map.put("touser", m.get("openid"));// 您要发送给某个用户的openid
            map.put("template_id", WeiXinConfig.lineId);// 模板消息id
            if(null == m.get("typeName"))m.put("typeName","暂无数据");
            keyword1Map.put("value", m.get("typeName"));
            keyword2Map.put("value", m.get("number"));
            keyword3Map.put("value", m.get("rn"));
            dataMap.put("first", firstMap);
            dataMap.put("keyword1", keyword1Map);
            dataMap.put("keyword2", keyword2Map);
            dataMap.put("keyword3", keyword3Map);
            dataMap.put("remark", remark);
            map.put("data", dataMap);
            HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());

        }
    }


    /**
     *
     * 异步调用群众电话信息并记录
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void getSMSInfo(String areaId, String number, String tell,int status) {
        mobileSilentService.insertValue(number, areaId, null,null, null,tell,status);
    }

    public String checkUserWx(String openid){
        String url = WeiXinConfig.weixin_user_info.replace("ACCESS_TOKEN", CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString()).replace("OPENID", openid);
        String result = HttpRequestUtil.sendPost(url, null);
        try{
            if (StringUtils.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.fromObject(result);
                if("1".equals(jsonObject.getString("subscribe"))){
                    return "已关注！";
                }
            }
        }catch (Exception e){
        }
        return null;
    }

    /**
     *
     * 通过areaId,number异步推送取号排队提醒 短信推送
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendSMSQueueInfo(String tell,String number,Map<String, Object> m) {
        if(StringUtils.isNotBlank(tell)){
            String content = "您的排队信息如下 \n排队编号："+number+" \n业务名称："+m.get("typeName")+" \n等待人数："+m.get("num")+" \n平均办理时间："
                    +m.get("waitTime")+"\n 发送时间："+DateUtils.getDateTime()+results;
            String args="CorpID="+name+"&Pwd="+pwd+"&Mobile="+tell+"&Content="+content+"&Cell=&SendTime=";
            HttpRequestUtil.sendPostTell(emsUrl, args);
        }
    }

    /**
     *
     * 排队叫号提醒 短信推送
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendSMSInfo(String number,String tell, String winNo){
        if(StringUtils.isNotBlank(tell)){
            String content = "请您手持排队编号"+number+"，尽快到"+winNo+"，办理该业务！ \n发送时间："+DateUtils.getDateTime()+results;
            String args="CorpID="+name+"&Pwd="+pwd+"&Mobile="+tell+"&Content="+content+"&Cell=&SendTime=";
            args = HttpRequestUtil.sendPostTell(emsUrl, args);
            //调用记录
            userService.intoUserHistory("无","排队叫号短信推送","返回结果:"+args,number,winNo,tell);
        }
    }

    /**
     *
     * 通过areaId,number异步推送排队提醒 短信推送
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendLineSMSInfo(Map<String,Object> m) {
        if(m.get("tell") != null && StringUtils.isNotBlank(String.valueOf(m.get("tell")))){
            String content = "您所办理的"+m.get("typeName")+"，还需等待"+m.get("rn")+"人办理。您的排队编号为"+m.get("number")+", 请您做好办理准备！ \n发送时间："+DateUtils.getDateTime()+results;
            String args="CorpID="+name+"&Pwd="+pwd+"&Mobile="+String.valueOf(m.get("tell"))+"&Content="+content+"&Cell=&SendTime=";
            HttpRequestUtil.sendPostTell(emsUrl, args);
        }
    }

    /**
     *
     * 通过areaId,number异步推送排队提醒 短信推送
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendSmsVerificationCode(String tell,String code) {
        String content = "感谢使用高馨办为您服务，您的验证码："+code+"\n谢谢您的使用！"+results;
        String args="CorpID="+name+"&Pwd="+pwd+"&Mobile="+tell+"&Content="+content+"&Cell=&SendTime=";
        HttpRequestUtil.sendPostTell(emsUrl, args);
    }

    /**
     *
     * 差评警报推送
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2023-02-10
     */

    public void toManagerPJ(String id,String remark,String myd) {
        Map<String,Object> map = new HashMap();
        HashMap <String, String> wxpjValue = mobileSilentService.findWXPJValue(id);
        if(null != wxpjValue && null != wxpjValue.get("tell")){
            map.put("template_id", WeiXinConfig.diffId);// 模板消息id
            Map<String, String> firstMap = new HashMap<String, String>();
            firstMap.put("value", "您好评价信息如下："); // 内容value
            firstMap.put("color", "#173177"); // 字体颜色
            //评价事项
            Map<String, String> twoMap = new HashMap<String, String>();
            twoMap.put("value", wxpjValue.get("typeName")+"["+wxpjValue.get("areaName")+"]"); // 内容value
            twoMap.put("color", "#888888"); // 字体颜色
            //办理编号
            Map<String, String> threeMap = new HashMap<String, String>();
            threeMap.put("value", wxpjValue.get("number")); // 内容value
            threeMap.put("color", "#888888"); // 字体颜色
            //满意度
            Map<String, String> fourMap = new HashMap<String, String>();
            fourMap.put("value", myd); // 内容value
            fourMap.put("color", "#888888"); // 字体颜色
            //评价时间
            Map<String, String> fiveMap = new HashMap<String, String>();
            fiveMap.put("value", DateUtils.getDateTime()); // 内容value
            fiveMap.put("color", "#888888"); // 字体颜色
            //备注
            Map<String, String> remarkMap = new HashMap<String, String>();
            remarkMap.put("value", remark); // 内容value
            remarkMap.put("color", "#888888"); // 字体颜色
            // 封装data
            Map<String, Map> dataMap = new HashMap<String, Map>();
            dataMap.put("first", firstMap);
            dataMap.put("keyword1", twoMap);
            dataMap.put("keyword2", threeMap);
            dataMap.put("keyword3", fourMap);
            dataMap.put("keyword4", fiveMap);
            dataMap.put("remark", remarkMap);
            map.put("data", dataMap);
            map.put("touser", cpOpenid);// 您要发送给某个用户的openid
            map.put("url",PJURLXQ+id);
            HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());
        }
    }

    /**
     *
     * 通过areaId,number异步推送排队提醒 短信推送
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    public void toUserPJ(String openid,String number,String typeName,String id) {

        if(StringUtils.isBlank(openid))return;
        Map<String,Object> map = new HashMap();
        map.put("template_id", WeiXinConfig.pjId);// 模板消息id
        map.put("url",PJURL+id);
        // 封装data
        Map<String, Map> dataMap = new HashMap<String, Map>();
        Map<String, String> firstMap = new HashMap<String, String>();
        firstMap.put("value", "您的票号已经办理完成，您尚未在办理窗口进行评价！"); // 内容value
        firstMap.put("color", "#173177"); // 字体颜色
        // 封装remark 备注
        Map<String, String> remark = new HashMap<String, String>();
        remark.put("value", "请您点击进入进行网上评价！");
        remark.put("color", "#888888");
        // 封装keyword1 办理业务
        Map<String, Object> keyword1Map = new HashMap<String, Object>();
        keyword1Map.put("color", "#888888");
        // 封装keyword2 排队时间
        Map<String, Object> keyword2Map = new HashMap<String, Object>();
        keyword2Map.put("color", "#888888");
        // 封装keyword3 排队号码
        Map<String, Object> keyword3Map = new HashMap<String, Object>();
        keyword3Map.put("color", "#888888");
        map.put("touser", openid);// 您要发送给某个用户的openid
        keyword1Map.put("value", number);
        keyword2Map.put("value", typeName);
        keyword3Map.put("value", DateUtils.getDateTime());
        dataMap.put("first", firstMap);
        dataMap.put("keyword1", keyword1Map);
        dataMap.put("keyword2", keyword2Map);
        dataMap.put("keyword3", keyword3Map);
        dataMap.put("remark", remark);
        map.put("data", dataMap);
        HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());
    }

    /**
     *
     * 综窗业务推送
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendLineWXZF(String title,String doTime,String typeName,String status,String openid) {
        Map<String,Object> map = new HashMap();
        // 封装data
        Map<String, Map> dataMap = new HashMap<String, Map>();
        Map<String, String> firstMap = new HashMap<String, String>();
        firstMap.put("value", title); // 内容value
        firstMap.put("color", "#173177"); // 字体颜色
        // 封装keyword1 业务类型
        Map<String, Object> keyword1Map = new HashMap<String, Object>();
        keyword1Map.put("color", "#888888");
        // 封装keyword2 排队号码
        Map<String, Object> keyword2Map = new HashMap<String, Object>();
        keyword2Map.put("color", "#888888");
        // 封装keyword3 前面人数
        Map<String, Object> keyword3Map = new HashMap<String, Object>();
        keyword3Map.put("color", "#888888");
        // 封装remark 备注
        Map<String, String> remark = new HashMap<String, String>();
        map.put("touser", openid);// 您要发送给某个用户的openid
        map.put("template_id", WeiXinConfig.zcId);// 模板消息id
        keyword1Map.put("value", doTime);
        keyword2Map.put("value", typeName);
        keyword3Map.put("value", status);
        dataMap.put("first", firstMap);
        dataMap.put("keyword1", keyword1Map);
        dataMap.put("keyword2", keyword2Map);
        dataMap.put("keyword3", keyword3Map);
        dataMap.put("remark", remark);
        map.put("data", dataMap);
        HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());

    }

    /**
     * 短信发送接口
     * @param phoneNumber 国内手机号(此方法会在号码前添加+86)
     * @param value 数据封装
     * @return SendStatus.getCode()=="Ok"，表示发送成功
     * @throws TencentCloudSDKException
     */
    public static SendStatus SendResultSL(String phoneNumber, String[] value, String id){
        SysConfig config = SpringContextHolder.getBean(SysConfigService.class).get(id);
        // TODO 需要修改到配置文件
        Credential cred = new Credential(config.getTencentApiSecretId(), config.getTencentApiSecretKey());

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "", clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setPhoneNumberSet(new String[]{"+86"+phoneNumber});
        //验证码模板ID
        req.setTemplateID(config.getTencentSmsVerificationCodeTemplateID());// TODO 需要修改到配置文件
        //审核通过的签名
        req.setSign(config.getTencentSmsSign());// TODO 需要修改到配置文件
        req.setTemplateParamSet(value);
        //短信应用ID
        req.setSmsSdkAppid(config.getTencentSmsSdkAppid());//TODO 需要修改到配置文件
        try {
            SendSmsResponse resp = client.SendSms(req);
            return resp.getSendStatusSet()[0];
        }catch (Exception e){
            e.printStackTrace();
            logger.error("tell:"+phoneNumber+" reason:"+e.getMessage());
        }
        return null;
    }

    /**
     * checkUserYY 获取用户预约业务
     *
     * @author azj
     * @version 2020-04-24
     */
    public AjaxJson checkUserYY(String orgrizeId, String idcard) {
        if(StringUtils.isNotBlank(orgrizeId) && StringUtils.isNotBlank(idcard)){
            MobileUserYY mu = mobileQueueService.checkUserYY(idcard, orgrizeId);
            if (null != mu && StringUtils.isNotBlank(mu.getId())) {
                logger.error("【"+idcard+"】预约验证"+mu.toString());
                return AjaxJson.success("操作成功！").put("data", mu);
            }else{
                return AjaxJson.error("当前时间段暂无预约业务!").put("code", 400);
            }
        }else{
            return AjaxJson.error("身份证号失败!").put("code", 400);
        }
    }

    /**
     * updateUserYY 更新用户预约状态
     *
     * @author azj
     * @version 2020-04-24
     */
    public Integer updateUserYY(String id, String number) {
        if (StringUtils.isNotBlank(id) && StringUtils.isNotBlank(number)) {
            return mobileQueueService.updateUserYY(id, number);
        }else{
            return 0;
        }
    }

    /**
     *公安同步叫号，结束，弃号状态
     *
     * @return
     */
    @Async()
    public void queueTimeSynchronous(int type,String number,String url,String winNo){
        Map<String, Object> startTime = mobilePublicSecurityService.getStartTime(number);
        String date = DateUtils.formatDate(new Date(), DateUtils.PATTERN_YYYYMMDD);
        long time = new Date().getTime();
        int index = winNo.indexOf("号窗口");
        if(index!=-1){
            winNo=winNo.substring(0,index);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sdate = null;
        try {
            sdate = sdf.parse(startTime.get("stime")+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        long time1 = sdate.getTime();
        String stime = String.valueOf(time1);
        String timeValue = String.valueOf(time);
        String typeValue = String.valueOf(type);
        startTime.put("date",date);
        startTime.put("time",timeValue);
        startTime.put("type",typeValue);
        startTime.put("stime",stime);
        String sign=typeValue;
        sign+=date;
        sign+=startTime.get("number");
        sign+=winNo;
        sign+=timeValue;
        sign+=stime;
        sign+=gongAnKey;
        sign = DigestUtils.md5DigestAsHex(sign.getBytes()).toUpperCase();
        startTime.put("sign",sign);
        String s = HttpQueueUtils.QueueSync(startTime, url);
        logger.error("公安同步叫号，结束，弃号状态type:"+typeValue+"number:" + number +" result: "+s);

    }

    /**
     * toQueueValuesOne 当前叫号数据推送
     * areaId点位标识
     * number排队编号
     * winNo窗口号
     * @author azj
     * @version 2020-04-24
     */
    @Async()
    public void toQueueValuesOne(String areaId, String number, String winNo,String areaNameValue) {
        if (StringUtils.isNotBlank(number) && StringUtils.isNotBlank(areaId) && StringUtils.isNotBlank(winNo)) {
            //发送当前叫号信息
            String numberValue;
            List<Map<String,Object>> map =  mobileSilentService.selectQueueByNumber(areaId,number);
            if(map != null && map.size() >= 1){
                for(Map<String,Object> m : map){
                    numberValue = number;
                    if(null != m.get("status") && "0".equals(String.valueOf(m.get("status")))){
                        if(StringUtils.isNotBlank(areaNameValue)){
                            numberValue = number+"("+areaNameValue+")";
                        }
                        //短信推送
                        if(null != m.get("tell") && StringUtils.isNotBlank(String.valueOf(m.get("tell"))) && String.valueOf(m.get("tell")).length() >= 10){
                            int count = mobileSilentService.getTellCount(String.valueOf(m.get("tell")),areaId,number);
                            if(count == 0){
                                mobileSilentService.intoTellCount(String.valueOf(m.get("tell")),DateUtils.getDateTime(),number,areaId);
                                this.sendSMSInfo(numberValue,String.valueOf(m.get("tell")),winNo);
                            }

                        }
                        //微信推送
                        if(null != m.get("openid") && StringUtils.isNotBlank(String.valueOf(m.get("openid")))){
                            this.sendWX(m,winNo,numberValue);
                        }
                    }
                }
            }else{
                logger.error("number：" + number+" reason : get result  is null!");
            }
        } else {
            logger.error("number：" + number+" reason : areaId  or number or winNo is null!");
        }
    }

    /**
     * toQueueValuesTwo 推送后3，6顺位排号数据推送
     * areaId点位标识
     * number排队编号
     * winNo窗口号
     * @author azj
     * @version 2020-04-24
     */
    @Async()
    public void toQueueValuesTwo(String areaId, String number, String winNo,String areaNameValue) {
        if (StringUtils.isNotBlank(number) && StringUtils.isNotBlank(areaId) && StringUtils.isNotBlank(winNo)) {
                //查询当前编号后 3，6位 异步发送提醒信息
                List<Map<String,Object>> maps = mobileSilentService.selectLineByNumber(number,areaId);
                if(null != maps){
                    for(Map<String,Object> ms : maps){
                        if(null != ms.get("status") && "0".equals(String.valueOf(ms.get("status")))){
                            //异步调用
                            if(null != ms.get("rn")){
                                ms.put("rn",Integer.parseInt(String.valueOf(ms.get("rn")))-1);
                            }
                            if(StringUtils.isNotBlank(areaNameValue)){
                                ms.put("number",ms.get("number")+"("+areaNameValue+")");
                            }
                            this.sendLineWX(ms);
                            //this.sendLineSMSInfo(ms);
                        }
                    }
                }
        } else {
            logger.error("number：" + number+" reason : areaId  or number or winNo is null!");
        }
    }

    /**
     * intoUserHistory 添加调用记录
     * areaId点位标识
     * number排队编号
     * winNo窗口号
     * @author azj
     * @version 2020-04-24
     */
    @Async()
    public void intoUserHistory(int type,int waitNum,String tell,String areaId,String number,String winNo,String userNo,String idcard,String yyID,String result){
        mobileSilentService.intoUserHistory(type, waitNum, tell, areaId, number, winNo, userNo, idcard, yyID,result);
    }

    /**
     * checkSHBZK 电子社保认证
     * areaId点位标识
     * number排队编号
     * winNo窗口号
     * @author azj
     * @version 2020-04-24
     */
    @Async()
    public void intoSHBZKHistory(String Qrcode){
        mobileIndexService.intoQrcode(Qrcode,"排队机扫码");
    }


    /**
     *
     * 推送当前微信用户评价提醒
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void sendPJWX(String areaId,String number,String winNo) {
        String openid = mobilebusinessService.findPJWXID(areaId,number);
        if(StringUtils.isBlank(openid))return;
        this.getWXInfo();
        Map<String,Object> map = new HashMap();
        map.put("template_id", WeiXinConfig.pjId);// 模板消息id
        // 封装data
        Map<String, Map> dataMap = new HashMap<String, Map>();
        Map<String, String> firstMap = new HashMap<String, String>();
        firstMap.put("value", "您的票号已经办理完成，请为"+winNo+"进行评价！"); // 内容value
        firstMap.put("color", "#173177"); // 字体颜色
        // 封装remark 备注
        Map<String, String> remark = new HashMap<String, String>();
        remark.put("value", "请在评价仪上进行有效评价！");
        remark.put("color", "#888888");
        // 封装keyword1 办理业务
        Map<String, Object> keyword1Map = new HashMap<String, Object>();
        keyword1Map.put("color", "#888888");
        // 封装keyword2 排队时间
        Map<String, Object> keyword2Map = new HashMap<String, Object>();
        keyword2Map.put("color", "#888888");
        // 封装keyword3 排队号码
        Map<String, Object> keyword3Map = new HashMap<String, Object>();
        keyword3Map.put("color", "#888888");
        map.put("touser", openid);// 您要发送给某个用户的openid
        keyword1Map.put("value", number);
        keyword2Map.put("value", mobilebusinessService.findTypeNameValue(areaId,number));
        keyword3Map.put("value", DateUtils.getDateTime());
        dataMap.put("first", firstMap);
        dataMap.put("keyword1", keyword1Map);
        dataMap.put("keyword2", keyword2Map);
        dataMap.put("keyword3", keyword3Map);
        dataMap.put("remark", remark);
        map.put("data", dataMap);
        HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());
    }

    /**
     * 蓉易办-排队叫号-区县上传取号数据
     * @param qno 排队叫号编号
     * @param areacode 区划编码syson
     */
    @Async()
    public void estabgetQueuingUp(String qno,String areacode){
        Map<String,String> map=new HashMap<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String format = df.format(new Date());
        map.put("newTime",format);
        map.put("qno",qno);
        map.put("areacode",areacode);
        Map<String, String> estabgetQueuingUp = rongYiBanService.estabgetQueuingUp(map);
        if(null != estabgetQueuingUp){
            JSONObject jsonObject = JSONObject.fromObject(estabgetQueuingUp);
            jsonObject.put("areacode",AreaCode);
            jsonObject.put("tasktypeguid",AreaCode+jsonObject.get("tasktypeguid"));
            jsonObject.put("qno",getJCNumber(areacode,qno));
            logger.error("蓉易办-排队叫号-区县上传取号数据:"+qno+":"+jsonObject.toString());
            String data = RongYiBanOkHttpUtil.getData(AppKey, GetNumber, jsonObject.toString());
            logger.error("蓉易办-排队叫号-区县上传取号数据接口返回:"+qno+":"+data);
            List<String> list=new ArrayList<>();
            list.add(estabgetQueuingUp.get("id"));
            String s = JSONObject.fromObject(data).getString("data");
            s =JSONObject.fromObject(s).getJSONObject("custom").getString("text");
            if("".equals(s)){
                rongYiBanService.updateBak("1",list);
            }else {
                rongYiBanService.updateBak(s,list);
            }
            this.estabgetIsCall(qno,areacode);
        }

    }

    /**
     * 蓉易办-排队叫号-区县叫号信息上传
     * @param qno 排队叫号编号
     * @param areacode 区划编码syson
     */
    public void estabgetIsCall(String qno,String areacode){
        Map<String,String> map=new HashMap<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String format = df.format(new Date());
        map.put("newTime",format);
        map.put("qno",qno);
        map.put("areacode",areacode);
        Map<String, String> estabgetIsCall = rongYiBanService.estabgetIsCall(map);
        if(null != estabgetIsCall){
            JSONObject jsonObject = JSONObject.fromObject(estabgetIsCall);
            jsonObject.put("iscall",1);
            jsonObject.put("areacode",AreaCode);
            jsonObject.put("tasktypeguid",AreaCode+jsonObject.get("tasktypeguid"));
            jsonObject.put("qno",getJCNumber(areacode,qno));
            logger.error("蓉易办-排队叫号-区县叫号信息上传:"+qno+":"+jsonObject.toString());
            String data = RongYiBanOkHttpUtil.getData(AppKey, HandleInfoUp, jsonObject.toString());
            logger.error("蓉易办-排队叫号-区县叫号信息上传接口返回:"+qno+":"+data);
            List<String> list=new ArrayList<>();
            list.add(estabgetIsCall.get("id"));
            String s = JSONObject.fromObject(data).getString("data");
            s =JSONObject.fromObject(s).getJSONObject("custom").getString("text");
            if("".equals(s)){
                rongYiBanService.updateBak("2",list);
            }

        }

    }
    /**
     * 蓉易办-排队叫号-区县办结信息上传
     * @param qno 排队叫号编号
     * @param areacode 区划编码syson
     */
    @Async()
    public void estabgetBanJie(String qno,String areacode){
        Map<String,String> map=new HashMap<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String format = df.format(new Date());
        map.put("newTime",format);
        map.put("qno",qno);
        map.put("areacode",areacode);
        Map<String, String> estabgetIsCall = rongYiBanService.estabgetIsCall(map);
        if(null != estabgetIsCall){
            JSONObject jsonObject = JSONObject.fromObject(estabgetIsCall);
            jsonObject.put("iscall",2);
            jsonObject.put("areacode",AreaCode);
            jsonObject.put("tasktypeguid",AreaCode+jsonObject.get("tasktypeguid"));
            jsonObject.put("qno",getJCNumber(areacode,qno));
            logger.error("蓉易办-排队叫号-区县办结信息上传:"+qno+":"+jsonObject.toString());
            String data = RongYiBanOkHttpUtil.getData(AppKey, HandleInfoUp, jsonObject.toString());
            logger.error("蓉易办-排队叫号-区县办结信息上传接口返回:"+qno+":"+data);
            List<String> list=new ArrayList<>();
            list.add(estabgetIsCall.get("id"));
            String s = JSONObject.fromObject(data).getString("data");
            s =JSONObject.fromObject(s).getJSONObject("custom").getString("text");
            if("".equals(s)){
                rongYiBanService.updateBak("3",list);
            }
        }

    }
    public String getJCNumber(String sysno,String qno){
        String jc = rongYiBanService.getJC(sysno);

        return jc+qno;
    }
    /**
     * 记录取号数据
     */
    @Async()
    public void intoWaitPersonHistory(String areaId,String number,String tell,int waitNum,String idcard){
        int type = 0;
        if(StringUtils.isNotBlank(idcard) && mobileLoginService.findUserByIdacrd(idcard) >= 1)type = 1;
        swService.intoWaitPerson(DateUtils.getDateTime(),waitNum,tell,areaId,number,type);
    }

    /**
     * 2F-A区市场监管 语音呼叫
     */
    @Async()
    public String setVoiceByIp(String context){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "<?xml version=\"1.0\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"><SOAP-ENV:Body xmlns:NS1=\"urn:etyy_dataIntf-Ietyy_data\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><NS1:getCmd><s xsi:type=\"xsd:string\">{\"cmd\":1001,\"data\":{\"host\":\"10.31.172.11\",\"params\":{\"isQZ\":\"1\",\"context\":\""+context+"\",\"num\":1,\"APKVIDEO\":{\"isPercent\":1,\"ip\":\"127.0.0.1\",\"s\":0.4,\"v\":0.9,\"toip\":\"127.0.0.1\"}}}}</s></NS1:getCmd></SOAP-ENV:Body></SOAP-ENV:Envelope>");
        Request request = new Request.Builder()
                .url(voice)
                .method("POST", body)
                .addHeader("Content-Type", "text/plain")
                .build();
        try{
            Response response = client.newCall(request).execute();
            return response.body().string().toString();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("get street is error ："+e.getMessage());
        }
        return null;
    }
    /**
     * 超60岁老年人取号短信提醒
     * @param tell 群众电话
     * @param areaId 区域编号
     * @param number 排队号码
     */
    @Async()
    public void sendWarnSMSInfo(String tell,String areaId,String number) {
        Map<String,String> textMessage = textMessageWarnService.getTextMessage(areaId);
        if(tell != null && StringUtils.isNotBlank(String.valueOf(tell))  && StringUtils.isNotBlank(String.valueOf(textMessage.get("textMessageOldMan")))){
            String content = "尊敬的申请人您好，成都高新区"+textMessage.get("areaName")+",可提供帮办代办服务，如您需要帮助，可致电帮办代办咨询电话。咨询电话："+textMessage.get("textMessageOldMan")+" \n发送时间："+DateUtils.getDateTime()+results;
            String args="CorpID="+name+"&Pwd="+pwd+"&Mobile="+String.valueOf(tell)+"&Content="+content+"&Cell=&SendTime=";
            HttpRequestUtil.sendPostTell(emsUrl, args);

            String contentContacts = "成都高新区"+textMessage.get("areaName")+"有老年人取号, 联系方式："+tell+"，请帮办人员与老年人联系，询问是否需要帮办。 \n发送时间："+DateUtils.getDateTime()+results;
            String argsContacts="CorpID="+name+"&Pwd="+pwd+"&Mobile="+String.valueOf(textMessage.get("textMessageOldManPhone"))+"&Content="+contentContacts+"&Cell=&SendTime=";
            HttpRequestUtil.sendPostTell(emsUrl, argsContacts);
        }
    }

    /**
     *
     * 异步验证微信token有效性并推送通知
     * <p>
     *
     * </p>
     *
     * @return
     * @author azj 2021-02-10
     */
    @Async()
    public void getWXInfoAsync(String openid,String number,Map<String, Object> m,String tell,String areaId) {
        //验证token有效性
        String url = WeiXinConfig.weixin_user_info.replace("ACCESS_TOKEN", CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString()).replace("OPENID", CS_OPENID);
        String result = "";
        try{
            result = HttpRequestUtil.sendPost(url, null);
            if (result == null || result.contains("errcode")) {
                weixinUtil.getAccessToken();
            }
        }catch (Exception e){
            logger.error("微信token失效！");
        }finally {
            if(null != m){
                Map<String,Object> map = new HashMap();
                map.put("template_id", WeiXinConfig.successQueueId);// 模板消息id
                if(number.indexOf("(") >= 0){
                    number = number.substring(0,number.indexOf("("));
                }
                //isSend = 0 已发送
                map.put("url",toUrl+"?isSend="+WeiXinConfig.isSend+"&areaId="+areaId+"&number="+number+"&tell="+tell);
                // 封装data
                Map<String, Map> dataMap = new HashMap<String, Map>();
                Map<String, String> firstMap = new HashMap<String, String>();
                firstMap.put("value", "尊敬的群众您好，排队信息如下（点击可查看当前时时排队情况）"); // 内容value
                firstMap.put("color", "#173177"); // 字体颜色
                // 封装remark 备注
                Map<String, String> remark = new HashMap<String, String>();
                remark.put("value", "目前大厅已启用无声叫号，请关注大厅叫号信息。以免过号，谢谢合作!");
                remark.put("color", "#888888");
                // 封装keyword1 办理业务
                Map<String, Object> keyword1Map = new HashMap<String, Object>();
                keyword1Map.put("color", "#888888");
                // 封装keyword2 排队时间
                Map<String, Object> keyword2Map = new HashMap<String, Object>();
                keyword2Map.put("color", "#888888");
                // 封装keyword3 排队号码
                Map<String, Object> keyword3Map = new HashMap<String, Object>();
                keyword3Map.put("color", "#888888");
                // 封装keyword4 等待人数
                Map<String, Object> keyword4Map = new HashMap<String, Object>();
                keyword4Map.put("color", "#888888");
                // 封装keyword5 办理地点
                Map<String, Object> keyword5Map = new HashMap<String, Object>();
                keyword5Map.put("color", "#888888");

                map.put("touser", openid);// 您要发送给某个用户的openid
                keyword1Map.put("value", m.get("typeName"));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                keyword2Map.put("value", sdf.format(new Date()));
                Map <String, String> numberValue = mobileQueueService.findNumberValue(areaId, number);
                if(null != numberValue){
                    keyword3Map.put("value", number+"（"+getTM(numberValue.get("name"),numberValue.get("card"))+"）");
                }else{
                    keyword3Map.put("value", number);
                }
                keyword4Map.put("value", m.get("num"));
                keyword5Map.put("value", m.get("areaName"));
                dataMap.put("first", firstMap);
                dataMap.put("keyword1", keyword1Map);
                dataMap.put("keyword2", keyword2Map);
                dataMap.put("keyword3", keyword3Map);
                dataMap.put("keyword4", keyword4Map);
                dataMap.put("keyword5", keyword5Map);
                dataMap.put("remark", remark);
                map.put("data", dataMap);
                result = HttpRequestUtil.getJsonData(JSONObject.fromObject(map), WeiXinConfig.sendMessage+CacheUtils.get(WeixinUtil.TOKEN_CACHE, "token").toString());
                //调用记录
                userService.intoUserHistory(openid,"线下取号微信排队信息推送","返回结果:"+result,number,m.get("areaName").toString(),"无");
            }
        }
    }

    public static String getTM(String name,String card){
        if(name.length() <= 2){
            return name.substring(0,1)+"*";
        } else{
            String result = name.substring(0,1);
            for(int i=2;i<=name.length();i++){
                if(i == name.length()){
                    result+=name.substring(i-1,i);
                }else{
                    result+="*";
                }
            }
            return result+card.substring(14,18);
        }
    }

}
